﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using AcSDK.Util;

namespace AcSDK
{
    public class AcClient : DefaultAcClient
    {
        public string Post(AcDictionary parameters)
        {
            var http = new HttpHelper();
            parameters = InitParameters(parameters);

            return http.Post(URL, parameters);
        }

        public ClientConfig    Client { get;set;}

        public AcClient()
        {
            Client = new ClientConfig();
            this.CompanyId = Client.CompanyId;
            this.Appkey = Client.AppKey;
            this.SecretKey = Client.AppSecret;
            serverUrl = Client.Url;
        }


        public AcClient(ClientConfig clientConfig)
        {
            Client = clientConfig;

            this.CompanyId = clientConfig.CompanyId;
            this.Appkey = clientConfig.AppKey;
            this.SecretKey = clientConfig.AppSecret;
            serverUrl = clientConfig.Url;
        }

        public AcClient(string serverUrl, string appKey, string appSecret, long companyId)
            : base(serverUrl, appKey, appSecret)
        {
            this.CompanyId = companyId;
            this.Appkey = appKey;
            this.SecretKey = appSecret;
        }

        public AcClient(string serverUrl, string appKey, string appSecret, string format)
            : base(serverUrl, appKey, appSecret, format)
        {
        }

        public override AcDictionary InitParameters(AcDictionary parameters)
        {
            parameters.Add("CompanyId", CompanyId + "");
            parameters.Add("AppKey", Appkey);
            parameters.Add("Ver", Ver);
            //parameters.Add("Method", Method);
            parameters.Add("Timestamp", Timestamp);
            parameters.Add("Format", Format);

            //生成签名
            parameters.Add("Sign", GetSign(parameters));

            return parameters;
        }

        /// <summary>
        /// 生成签名函数
        /// </summary>
        private string GetSign(AcDictionary parameters)
        {
            var NewSign = "";

            // 按字母顺序排序
            IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal);

            // 把所有参数名和参数值串在一起
            StringBuilder query = new StringBuilder();

            foreach (KeyValuePair<string, string> kv in sortedParams)
            {
                if (!string.IsNullOrEmpty(kv.Key) && !string.IsNullOrEmpty(kv.Value))
                {
                    query.Append(kv.Key).Append(kv.Value);
                }
            }


            NewSign = NewSign + query.ToString().Trim().ToLower();
            NewSign = NewSign.Trim().ToLower();
            NewSign = HashString(NewSign + "mst");
            NewSign = HashString(NewSign + "mst");
            NewSign = HashString(NewSign + "mst" + SecretKey);

            return NewSign;

        }



        public string URL
        {
            get;
            set;

        }
        /// <summary>
        /// 每个厂家在码上通的
        /// </summary>
        public long CompanyId
        {
            get;
            set;

        }
        /// <summary>
        /// AppKey
        /// </summary>
        public string Appkey
        {
            get;
            set;
        }

        public string SecretKey
        {
            get;
            set;
        }

        /// <summary>
        /// 调用接口的版本当前为1.0
        /// </summary>
        public string Ver
        {
            get
            {
                return "1.0";
            }
        }

        /// <summary>
        /// 时间戳，格式为yyyy-MM-dd HH:mm:ss
        /// </summary>
        public string Timestamp
        {
            get
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        public string Method
        {
            get;
            set;
        }

        public string Format
        {
            get
            {
                return "json";
            }
        }

        /**/
        /// <summary>
        /// 将字符串加密
        /// </summary>
        /// <param name="sourceString">需要加密的字符串</param>
        /// <returns>MD5加密后字符串</returns>
        public string HashString(string sourceString)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sourceString, "MD5").ToLower();
        }

     
    }
}
